package com.jdk.learn.juc2.thread.excrise;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 类功能描述:
 * 等待线程A和B处理完业务
 *
 * @author bgt
 * @version V1.0
 * @since 2019-02-21 13:39
 */
public class PrintABBySemaphore2 {
    private static Semaphore conditionA = new Semaphore(1);
    private static Semaphore conditionB = new Semaphore(0);
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        Thread printA = new Thread(() -> {
            try {
                while (count.get()!=19) {
                    conditionA.acquire();
                    System.out.print("A");
                    count.incrementAndGet();
                    conditionB.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }

        });
        Thread printB = new Thread(() -> {
            try {
                Thread.sleep(1);
                while (count.get()!=20) {
                    conditionB.acquire();
                    System.out.print("B  ");
                    count.incrementAndGet();
                    conditionA.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        });
        printA.start();
        printB.start();
    }



}
